//@author A0096759E

/**
 * Deserializes Google Task JSON Objects into ScheduleEvents
 */

package googlecal;

import java.lang.reflect.Type;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import storage.ScheduleEvent;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

public class GoogleTaskItemDeserializer implements JsonDeserializer<ScheduleEvent>{
	private static final int EVENT_TYPE_FLOATING = 0;
	private static final int EVENT_TYPE_DEADLINE = 1;
	
	private static final String GOOGLE_TASK_IS_DONE = "completed";
	private static final String EVENT_TIME_END_MIDNIGHT = "2359";
	private static final String LIFESHELF_TASK_UNNAMED = "Unnamed Event";
	
	private static final String KEYWORD_ID = "id";
	private static final String KEYWORD_TITLE = "title";
	private static final String KEYWORD_NOTES = "notes";
	private static final String KEYWORD_STATUS = "status";
	private static final String KEYWORD_DUE = "due";
	private static final String KEYWORD_UPDATED = "updated";
	
	private static final String DATETIME_FORMAT_LOCAL_DATE = "ddMMyyyy";
	private static final String DATETIME_FORMAT_TIME = "HHmm";
	private static final String DATETIME_FORMAT_GTASK_DATETIME = "yyyy-MM-dd'T'HH:mm:ss'.000Z'";
	private static final char OPEN_SQUARE = '[';
	private static final char CLOSE_SQUARE = ']';
	
	
	  @Override
	  public ScheduleEvent deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
	      throws JsonParseException {
	    final JsonObject jsonObject = json.getAsJsonObject();

	    final ScheduleEvent googleCalendarItem = new ScheduleEvent();
	    googleCalendarItem.setGoogleUID(jsonObject.get(KEYWORD_ID).getAsString());
	    googleCalendarItem.setType(EVENT_TYPE_FLOATING);
	    
	    JsonElement googleTaskStatus = jsonObject.get(KEYWORD_STATUS);
	    if (googleTaskStatus.getAsString() != null && googleTaskStatus.getAsString().equals(GOOGLE_TASK_IS_DONE)) {
	    		try {
					googleCalendarItem.setIsDone();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	    }

	    JsonElement title = jsonObject.get(KEYWORD_TITLE);
	    if (title != null) {
	    	String googleTaskTitle = title.getAsString();
	    	if (googleTaskTitle != null) {
	    		if (googleTaskTitle.length() > 9 && googleTaskTitle.charAt(0) == OPEN_SQUARE && googleTaskTitle.charAt(8) == CLOSE_SQUARE) {
	    			DateTimeFormatter timeChecker = DateTimeFormat.forPattern(DATETIME_FORMAT_TIME);
	    			try {
		    	    	timeChecker.parseDateTime(googleTaskTitle.substring(4,8));
		    			googleCalendarItem.setEndTime(googleTaskTitle.substring(4,8));
		    			googleCalendarItem.setName(googleTaskTitle.substring(10));
	    			} catch (Exception e) {
		    			googleCalendarItem.setName(googleTaskTitle);
	    			}
	    		} else {
	    			googleCalendarItem.setName(googleTaskTitle);
	    		}
	    	} else {
	    		googleCalendarItem.setName(LIFESHELF_TASK_UNNAMED);
	    	}
	    } else {
	    	googleCalendarItem.setName(LIFESHELF_TASK_UNNAMED);
	    }
	    
	    
	    JsonElement due = jsonObject.get(KEYWORD_DUE);
	    DateTimeFormatter formatter = DateTimeFormat.forPattern(DATETIME_FORMAT_GTASK_DATETIME);
    	DateTimeFormatter dateFormat = DateTimeFormat.forPattern(DATETIME_FORMAT_LOCAL_DATE);
	    if (due != null) {
	    	DateTime dt = formatter.parseDateTime(due.getAsString());
	    	googleCalendarItem.setDate(dateFormat.print(dt));
			googleCalendarItem.setType(EVENT_TYPE_DEADLINE);
			if (googleCalendarItem.getEndTime() == null) {
		    	googleCalendarItem.setEndTime(EVENT_TIME_END_MIDNIGHT);
			}
	    } else if (googleCalendarItem.getEndTime() != null) {
			googleCalendarItem.setType(EVENT_TYPE_DEADLINE);
			googleCalendarItem.setDate(dateFormat.print(new DateTime()));
		} else {
	    	googleCalendarItem.setEndTime(null);
	    }
	    
	    
	    JsonElement localID = jsonObject.get(KEYWORD_NOTES);
	    if (localID != null) {
	    	googleCalendarItem.setUID(localID.getAsString());
	    }

	    googleCalendarItem.setUpdated(jsonObject.get(KEYWORD_UPDATED).getAsString());
	   
	    return googleCalendarItem;
	  }
}
